package com.ddxz.tool.core.convert;

import com.ddxz.tool.core.common.CommonUtil;
import net.sf.cglib.beans.BeanCopier;

import java.lang.reflect.Constructor;

/**
 * @author 朱宗刚
 * @version 1.0.0
 * @description com.casic.exchange.com.casic.common.tools.convert.BeanUtil
 * @date 2020/2/28
 */
public class BeanUtil {
    public static void copyProperties(Object src, Object target) {
        BeanCopier copier = BeanCopier.create(src.getClass(), target.getClass(), false);
        copier.copy(src, target, null);
    }

    public static void copyProperties(Object source, Object target, int type) {
        BeanCopier copier = BeanCopier.create(source.getClass(), target.getClass(), true);
        DdxzUtil converter = new DdxzUtil(type);
        copier.copy(source, target, converter);
    }

    public static void copyProperties(Object source, Object target, Class<?> destClass) {
        copyProperties(source, target, DdxzUtil.NONE_CONVERT, destClass);
    }

    public static void copyProperties(Object source, Object target, int type, Class<?> destClass) {
        BeanCopier copier = BeanCopier.create(source.getClass(), target.getClass(), true);
        DdxzUtil converter = new DdxzUtil(type, destClass);
        copier.copy(source, target, converter);
    }

    public static <T> T getTarget(Object src, Class<T> tar) {
        T t = CommonUtil.getInstance(tar);
        copyProperties(src, t);
        return t;
    }

    public static <T> T getTarget(Object src, Class<T> tar, int type) {
        T t = CommonUtil.getInstance(tar);
        copyProperties(src, t, type);
        return t;
    }

    public static <T> T getTarget(Object source, Class<T> tar, Class<?> destClass) {
        T t = CommonUtil.getInstance(tar);
        copyProperties(source, t, destClass);
        return t;
    }

    public static <T> T getTarget(Object source, Class<T> tar, Class<?> destClass, int type) {
        T t = CommonUtil.getInstance(tar);
        copyProperties(source, t, type, destClass);
        return t;
    }
}
